package com.hoshiicloud.user.service.impl.organization;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.user.entity.company.Company;
import com.hoshiicloud.user.entity.organization.ComOrganizationCategory;
import com.hoshiicloud.user.feign.IdGeneratorService;
import com.hoshiicloud.user.mapper.organization.ComOrganizationCategoryMapper;
import com.hoshiicloud.user.service.company.ICompanyService;
import com.hoshiicloud.user.service.organization.ComOrganizationCategoryService;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author:campchen
 * @Date:Created in 下午 04:11 2019/8/21
 * @PACKAGENAME com.hoshiicloud.user.service.impl.organization
 * @Description
 */
@Service
public class ComOrganizationCategoryServiceImpl extends ServiceImpl<ComOrganizationCategoryMapper, ComOrganizationCategory> implements ComOrganizationCategoryService {

    @Autowired
    private IdGeneratorService idGeneratorService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private ComOrganizationCategoryMapper comOrganizationCategoryMapper;

    /**
     * @Author:campchen
     * @Description:
     * @Date:上午 10:45 2019/6/14
     * @Param:
     * @return:
     */
    @Override
    @Cacheable(cacheNames = "ComOrganizationCategory", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "后台用户操作-根据供应商经营类目信息进行分页查询")
    public IPage<ComOrganizationCategory> getPage(IPage<ComOrganizationCategory> page, QueryWrapper<ComOrganizationCategory> queryWrapper, String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    /**
     * @param id
     * @return com.hoshiicloud.organization.entity.ComOrganizationCategory
     * @description: 根据id查询供应商经营类目com_organization_category
     */
    @Override
    @Cacheable(cacheNames = "ComOrganizationCategory", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "供应商信息-供应商经营类目com_organization_category -查询供应商经营类目com_organization_category ")
    public ComOrganizationCategory getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
     * @param entity
     * @return boolean
     * @description: 新增供应商经营类目com_organization_category
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "ComOrganizationCategory", allEntries = true)
    @Logable(authName = "供应商信息-供应商经营类目com_organization_category -新增供应商经营类目com_organization_category ")
    public boolean save(ComOrganizationCategory entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("新增失败"));
        }

        Company company = companyService.getById(entity.getCompanyId());
        if (null == company) {
            throw new ServiceException(CommonError.error("新增失败,租户有误"));
        }

        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
     * @param entity
     * @return boolean
     * @description: 修改供应商经营类目com_organization_category
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "ComOrganizationCategory", allEntries = true)
    @Logable(authName = "供应商信息-供应商经营类目com_organization_category -修改供应商经营类目com_organization_category ")
    public boolean updateById(ComOrganizationCategory entity) {
        entity.setUpdatedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Logable(authName = "供应商信息-供应商经营类目com_organization_category -批量新增供应商经营类目com_organization_category ")
    @CacheEvict(cacheNames = "ComOrganizationCategory", allEntries = true)
    public boolean insertCategory4ComOrganization(List<ComOrganizationCategory> comOrganizationCategories) {
        for (int i=0;i<comOrganizationCategories.size();i++){
            comOrganizationCategories.get(i).setCreatedTime(LocalDateTime.now());
            //1-未删除 0-已删除
            comOrganizationCategories.get(i).setDeleted(1);
            //1-可用 0-不可用
            comOrganizationCategories.get(i).setEnabled(1);
        }
        return comOrganizationCategoryMapper.insertCategory4ComOrganization(comOrganizationCategories);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "ComOrganizationCategory", allEntries = true)
    @Logable(authName = "供应商信息-供应商经营类目com_organization_category管理-批量物理删除经营领域com_organization_category")
    public boolean deletedByIds(List<Long> ids) {
        int row = baseMapper.deleteBatchIds(ids);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

}
